package logic.map
{
    import flash.display.*;
    import flash.geom.*;

    
    import flash.display.Bitmap;
    
    import Constants;
    

    
    public class MapBuilder
    {
        [Embed(source="../../../../resources/tiles.jpg")]
        private static const imgCls:Class;
        
        private static var imgObj:Bitmap;
        
        public static var levelArray:Array;
        
    
        public static function buildMapBitmap(level:int):BitmapData
        {
            if (!imgObj)
            {
                imgObj = new imgCls() as Bitmap;
            }
            
            var levelString:String = "level"+level;            
            levelArray = LevelMaps[levelString];

            var width:uint =  levelArray.length;
            var height:uint = levelArray[0].length;
            
            var mapBitmap:BitmapData = new BitmapData(Constants.TILE_SIZE*height, Constants.TILE_SIZE*width,false, 0x000000); 
            var tilex:uint;
            var tiley:uint;
            
            for (var i:int = 0; i < width; i++)
            {
                for (var j:int = 0; j < height; j++)
                {
                    if(levelArray[i][j] == 0)
                        continue;
                        
                    tilex = (uint)(levelArray[i][j] % 10) * (Constants.TILE_SIZE + 1); 
                    tiley = (uint)(levelArray[i][j] / 10) * (Constants.TILE_SIZE + 1);

                    mapBitmap.copyPixels(imgObj.bitmapData, 
                         new Rectangle(tilex , tiley, Constants.TILE_SIZE, Constants.TILE_SIZE), 
                         new Point(j*Constants.TILE_SIZE, i*Constants.TILE_SIZE));
                }
            }

            return mapBitmap;
        }
    }
}
